package com.zking.controller.hcy;

import com.zking.entity.Item;
import com.zking.service.hcy.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/Hcy2")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping("/data")
    @ResponseBody
    public Item getData(int id) {
        Item item = itemService.queryItem(id);
        return item;
    }

    @RequestMapping(value = "/selectItemAll", method = RequestMethod.GET,name = "商品查询" )
    public String selectItemAll(HttpServletRequest request) {
        List<Item> items= itemService.queryItems();
        request.setAttribute("items",items);
        return "hcy/goods";
    }

    @RequestMapping(value = "/updateItem", method = RequestMethod.GET,name = "修改商品" )
    public String updateItem(HttpServletRequest request) {
        String title2 = request.getParameter("title2");
        String price2 = request.getParameter("price2");
        String num2 = request.getParameter("num2");
        String brand2 = request.getParameter("brand2");
        String category2 = request.getParameter("category2");
        String categoryId2 = request.getParameter("categoryId2");

        int id = Integer.parseInt(request.getParameter("id"));

        boolean b = itemService.updateItem(id, title2, price2,num2,brand2,category2,categoryId2);
        System.out.println(b);
        return "redirect:selectItemAll";

    }

    @RequestMapping(value = "/addItem", method = RequestMethod.GET,name = "添加商品" )
    public String addItem(HttpServletRequest request) {
        String title = request.getParameter("title");
        String price = request.getParameter("price");
        int num = Integer.parseInt(request.getParameter("num"));
        String brand = request.getParameter("brand");
        String seller = request.getParameter("seller");
        String category = request.getParameter("category");
        String categoryId = request.getParameter("categoryId");
        boolean b = itemService.addItem(title,price,num,brand,seller,category,categoryId);
        return "redirect:selectItemAll";
    }

    @RequestMapping(value = "/del" , method = RequestMethod.GET, name = "删除")
    public String delGoods(String id) {
        boolean b = itemService.delGoods(id);
        return"redirect:selectItemAll";
    }


}
